package com.sky.service.impl;

import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.Usermapper;
import com.sky.service.ReportService;
import com.sky.vo.*;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.formula.functions.Odd;
import org.springframework.core.OrderComparator;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ReportServiceImpl<map> implements ReportService {
    private final com.sky.mapper.OrdersMapper OrdersMapper;
    private final Usermapper usermapper;
    private final OrdersMapper ordersMapper;

    public ReportServiceImpl(OrdersMapper OrdersMapper, Usermapper usermapper, OrdersMapper ordersMapper) {
        this.OrdersMapper = OrdersMapper;
        this.usermapper = usermapper;
        this.ordersMapper = ordersMapper;
    }

    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {

    //1.准备日期列表数据 datelist  ----> 近七日：5-14，5-15.。。。5-20
        List<LocalDate> dateList=getDateList(begin,end);

        //2.准备营业额列表数据 turnoverList
        List<Double> turnoverList=new ArrayList<>();
        //营业额=订单状态已完成的订单金额
        //查询orders表，条件：状态-已完成，下单时间

        dateList.forEach(date->{
            Map map = new HashMap();
            map.put("status", Orders.COMPLETED);
            map.put("beginTime",LocalDateTime.of(date, LocalTime.MIN));
            map.put("endTime",LocalDateTime.of(date, LocalTime.MAX));
           Double turnover= OrdersMapper.sumByMap(map);
           //用来处理营业额的情况
            turnover= turnover == null ? 0.0:turnover;
            turnoverList.add(turnover);
        });
        //3.构造TurnoverReportVO对象并返回
        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .turnoverList(StringUtils.join(turnoverList,","))
                .build();

    }

    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        // 1.构造dateList数据
        List<LocalDate> dateList=getDateList(begin,end);
        //2.构造newUserlist数据，新增用户列表
            List<Integer> newUserList=new ArrayList<>();
            //循环遍历日期列表统计每日的新增用户数--user
        //3.构造totalUserList数据，总用户列表
            List<Integer> totalUserList=new ArrayList<>();

        dateList.forEach(date->{
            Map map = new HashMap();
            map.put("beginTime",LocalDateTime.of(date, LocalTime.MIN));
            map.put("endTime",LocalDateTime.of(date, LocalTime.MAX));
            Integer newUser= usermapper.countByMap(map);
            newUserList.add(newUser);

            map.put("beginTime",null);
            Integer totalUser= usermapper.countByMap(map);
            totalUserList.add(totalUser);
        });

        //4.构造UserReportVO对象并返回

        return UserReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .totalUserList(StringUtils.join(totalUserList,","))
                .newUserList(StringUtils.join(newUserList,","))
                .build();
    }

    @Override
    public OrderReportVO orderStatistics(LocalDate begin, LocalDate end) {
        //1.获取日期列表苏话剧dateList
         List<LocalDate> dateList=getDateList(begin,end);
         List<Integer> orderCountList=new ArrayList<>();
         List<Integer> validOrderCountList=new ArrayList<>();
         //循环白能力日期泪飙进行统计
        Integer totalOrderCount=0;
        Integer validOrderCount=0;
        for (LocalDate date : dateList) {
            //2.获取每日总订单列表数 orderCountList
            Map map = new HashMap();
            map.put("beginTime",LocalDateTime.of(date, LocalTime.MIN));
            map.put("endTime",LocalDateTime.of(date, LocalTime.MAX));
            Integer totalOrder= ordersMapper.countByMap(map);
            orderCountList.add(totalOrder);
            //3.获取每日有效订单列表数 validOrderCountList
            //统计每日有效orderd数量，条件：状态=有效（已完成）、下单时间》=当天起始时间 and 下单时间 <=当天结束时间
            map.put("status", Orders.COMPLETED);
            Integer validOrder=  ordersMapper.countByMap(map);
            validOrderCountList.add(validOrder);


            //4.获取订单总数  totalOrderCount
             totalOrderCount+=totalOrder;
            //5.获取有效订单数 validOrderCount
             validOrderCount+=validOrder;
        }


        //6.计算完成率 orderCompletionRate
        Double orderCompletionRate=0.0;
        if (totalOrderCount != 0) {
            orderCompletionRate = (validOrderCount +0.0) / totalOrderCount;
        }
        //7.封装OrderReportVO对象并返回
         return OrderReportVO.builder()
                 .dateList(StringUtils.join(dateList,","))
                 .orderCountList(StringUtils.join(orderCountList,","))
                 .validOrderCountList(StringUtils.join(validOrderCountList,","))
                 .validOrderCount(validOrderCount)
                 .totalOrderCount(totalOrderCount)
                 .orderCompletionRate(orderCompletionRate)
                 .build();

//        return null;
    }

    @Override
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {
        //1. 构造nameList,商品名称列表
        List<String> nameList = new ArrayList<>();
        //2. 构造numberList,商品销量（份数） 列表
        List numberList = new ArrayList<>();
        //查询订单明细表 order_detail + 订单表 orders,条件：订单状态-已完成，下单时间
        Map map = new HashMap();
        map.put("beginTime", LocalDateTime.of(begin, LocalTime.MIN));
        map.put("endTime", LocalDateTime.of(end, LocalTime.MAX));
        map.put("status", Orders.COMPLETED);


        List<Map> list = ordersMapper.sumTop10(map);
        for (Map m : list) {

            nameList.add((String) m.get("name"));
            Object number=  m.get("sumNum");
            numberList.add(number);
        }
      //3.封装 SaleTop10ReortVO 对象并返回
        return  SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(nameList,","))
                .numberList(StringUtils.join(numberList,","))
                .build();
    }


    private List<LocalDate> getDateList(LocalDate begin, LocalDate end ) {
    List<LocalDate> dateList=new ArrayList<>();
    //循环插入日期数据
    while(!begin.isAfter(end)) {
        //注意：小心死循环
        dateList.add(begin);
        begin=begin.plusDays(1);
    }
    log.info("dateList:{}",dateList);
        return dateList;
}
}
